﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-                                           *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月22日 18:30:34
 *  文件描述:发送邮件
 *****************************************************************************/

namespace Util
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using UnityEngine;

    public class MailHelper
    {
        //1.不要用傻逼的163邮箱
        //2.选择.Net 2.0 是不 .Net 2.0 Subset
        private static string mEmailAcount = "421107903@qq.com"; //邮箱

        private static string mEmailPassword = "mima"; //授权码
        private const string mHost = "smtp.qq.com"; //发送服务器

        private static List<string> mReceiver = new List<string>() //发送给的人
        {
            " ",
        };

        public static void Init(string email, string pass, string scndEmail)
        {
            mEmailAcount = email;
            mEmailPassword = pass;
            mReceiver = new List<string>() { scndEmail };
        }

        //标题//内容
        public static void SendMail(string subject, string body, string AttachmentPaths, Action<bool> onSend)
        {
            SendMail(subject, body, new List<Attachment>() { new Attachment(AttachmentPaths) }, onSend);
        }

        //标题//内容
        public static void SendMail(string subject, string body, List<Attachment> Attachments, Action<bool> onSend)
        {
            MailMessage message = new MailMessage();
            //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
            MailAddress fromAddr = new MailAddress(mEmailAcount);
            message.From = fromAddr;
            //设置收件人,可添加多个,添加方法与下面的一样
            mReceiver.ForEach(r => message.To.Add(r));

            //设置邮件标题
            message.Subject = subject;
            message.SubjectEncoding = Encoding.UTF8; //标题格式为UTF8
            //设置邮件内容
            message.Body = body;
            message.BodyEncoding = Encoding.UTF8; //内容格式为UTF8
            message.Priority = MailPriority.High;
            //获取附件
            for (int i = 0; i < Attachments.Count; i++)
            {
                message.Attachments.Add(Attachments[i]);
            }
            //message.Attachments.Add(new Attachment("screen.png"));

            //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是阿里云的
            SmtpClient client = new SmtpClient(mHost);

            //启用ssl,也就是安全发送
            client.EnableSsl = false;

            ServicePointManager.ServerCertificateValidationCallback =
                delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
            //发送邮件
            client.SendCompleted += new SendCompletedEventHandler((sender, arg) =>
            {
                bool result = arg.Error == null;

                if (!result)
                {
                    Debug.LogError(arg.Error);
                }

                if (onSend != null)
                {
                    onSend(result);
                }
            });
            //client.UseDefaultCredentials = true;
            //设置发送人的邮箱账号和密码
            client.Credentials = new NetworkCredential(mEmailAcount, mEmailPassword) as ICredentialsByHost;
            client.SendAsync(message, null);
        }
    }
}